﻿Imports System.Data.SqlClient
Imports System.Data
Public Class Calendar
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        myHeading.InnerText = Montly_Calendar.TodaysDate.ToString("dd MMMM")
        Montly_Calendar.SelectedDate = Montly_Calendar.TodaysDate
        Add_Appointment.Visible = True
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM Appointments WHERE EventDate = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Montly_Calendar.TodaysDate)
        'Getting records (only 1 line for selected Bibliophile)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            myHeading_Appointments.InnerText = "Your Appointments"
            GridView1.Visible = True

        Else
            myHeading_Appointments.InnerText = "No Appointments"
            GridView1.Visible = True
        End If
        dr.Close()
        conn.Close()
    End Sub


    Protected Sub Montly_Calendar_SelectionChanged(sender As Object, e As EventArgs) Handles Montly_Calendar.SelectionChanged
        myHeading.InnerText = Montly_Calendar.SelectedDate.ToString("dd MMMM")
        Add_Appointment.Visible = True

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM Appointments WHERE EventDate = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Montly_Calendar.SelectedDate)
        'Getting records (only 1 line for selected Bibliophile)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            myHeading_Appointments.InnerText = "Your Appointments"
            GridView1.Visible = True
            
        Else
            myHeading_Appointments.InnerText = "No Appointments"
            GridView1.Visible = False
        End If
        dr.Close()
        conn.Close()
    End Sub

    Protected Sub btn_AddAppointment_Click(sender As Object, e As EventArgs) Handles btn_AddAppointment.Click


        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("INSERT INTO Appointments(EventName, EventTime, EventDescription, EventDate) VALUES (@f1, @f2, @f3, @f4)", conn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_eventName.Text)
        cmd.Parameters.AddWithValue("@f2", ddl_Time.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f4", Montly_Calendar.SelectedDate)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        tb_eventName.Text = ""
        tb_Description.Text = ""
        ddl_Time.SelectedValue = 8
        GridView1.DataBind()
        GridView1.Visible = True
        myHeading_Appointments.InnerText = "Your Appointments"
    End Sub

    Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#efeeef'")
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'")
        End If
    End Sub

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim Label1 As Label = CType(e.Row.FindControl("Label1"), Label)
            Label1.Text = Label1.Text + ":00"
        End If
    End Sub

    Protected Sub GridView1_RowDeleted(sender As Object, e As GridViewDeletedEventArgs) Handles GridView1.RowDeleted
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM Appointments WHERE EventDate = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Montly_Calendar.TodaysDate)
        'Getting records (only 1 line for selected Bibliophile)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            myHeading_Appointments.InnerText = "Your Appointments"
            GridView1.Visible = True

        Else
            myHeading_Appointments.InnerText = "No Appointments"
            GridView1.Visible = True
        End If
        dr.Close()
        conn.Close()
    End Sub
End Class